#include "log/log.h"
#include "unistd.h"
#include "pthread.h"
pthread_t threads[10];
int a=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void * fun(void *)
{
      for (size_t i = 0; i < 2000; i++)
      {
        //解决竞态问题 加锁
        pthread_mutex_lock(&mutex);
        a++;//不是原子操作
        pthread_mutex_unlock(&mutex);
      }
      
}
int main(void)
{
    // 创建十个线程
    for (size_t i = 0; i < 10; i++)
    {
        pthread_create(&threads[i],NULL,fun,NULL);

    }

    //等待线程结束
    for (size_t i = 0; i < 10; i++)
    {
        pthread_join(threads[i],NULL);
    }
    log_info("a=%d",a);
    return 0;
    
}